﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HZ.Data;
using System.Data;
using System.Data.SqlClient;
using HZ.BidSystem.Model;


namespace HZ.BidSystem.DAL
{
    public class DAL_Company_Project_Switch
    {
        private static string GetStringList(IList<int> intlist)
        {
            bool first = true;
            StringBuilder sb = new StringBuilder();
            foreach (var item in intlist)
            {
                if (first)
                {
                    sb.Append(item);
                    first = false;
                }
                else
                {
                    sb.Append("," + item.ToString());
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 清空报价密码
        /// </summary>
        /// <param name="companylist"></param>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int ClearBidPassword(IList<int> companylist, int projectid)
        {
            if (System.Configuration.ConfigurationSettings.AppSettings["IsClearEncryptPrice"].ToString() == "1")
            {
                //客户端加密，利用企业输入的报价密码进行加密
                return HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(string.Format(@"UPDATE dbo.TB_Company_Project_Switch SET BidPassword = NULL,ConfirmStatus=0,ConfirmTime=null WHERE CompanyId IN ({0}) AND ProjectId = {1};
                UPDATE dbo.TB_Project_Bid SET EncryptPrice = NULL WHERE CompanyId IN ({0}) AND ProjectId = {1} ", GetStringList(companylist), projectid));
            }
            else
            {
                //中心端加密，利用中心key加解密
                return HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(string.Format(@"UPDATE dbo.TB_Company_Project_Switch SET BidPassword = NULL,ConfirmStatus=0,ConfirmTime=null WHERE CompanyId IN ({0}) AND ProjectId = {1}
", GetStringList(companylist), projectid));
            }
        }

        public static int ClearComfirmStatus(IList<int> companylist, int projectid)
        {
            return HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(string.Format("UPDATE dbo.TB_Company_Project_Switch SET ConfirmStatus = 0 WHERE CompanyId IN ({0}) AND ProjectId = {1}", GetStringList(companylist), projectid));
        }

        public static int ClearComfirmStatusAll(int projectid)
        {
            return HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(string.Format("UPDATE dbo.TB_Company_Project_Switch SET ConfirmStatus = 0 WHERE ProjectId = {0}", projectid));
        }

        public static int ResetPassword(IList<int> compnaylist, int projectid)
        {
            return HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(string.Format("UPDATE dbo.TB_Account SET [Password] = '{0}' WHERE AccountId IN ({1})", HZ.Fun.GetMd5("123456"), GetStringList(compnaylist)));
        }

        public static int ClearDecStatusAll(int projectid)
        {
            return HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(string.Format("UPDATE dbo.TB_Company_Project_Switch SET DecStatus = 0 , DecTime = NULL WHERE ProjectId = {0}", projectid));
        }
        /// <summary>
        /// 报价信息公示
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int CompanyMessageGongshi()
        {
            int i = 0;
            i= HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(string.Format("drop table AHCMS_DATA..ComPanyBaoJia;select * into AHCMS_DATA..ComPanyBaoJia from dbo.View_CompanyBaoJia"));
            return i;
        }
    }
}
